using System.Threading.Tasks;
using Matrix.Configuration.Events;

namespace Matrix.Configuration.Services
{
    /// <summary>
    /// 事件追踪器接口
    /// 负责事件的链路追踪和调试
    /// </summary>
    public interface IEventTracer
    {
        /// <summary>
        /// 开始追踪事件
        /// </summary>
        /// <param name="eventId">事件ID</param>
        /// <param name="eventType">事件类型</param>
        /// <param name="traceId">跟踪ID</param>
        /// <returns>追踪上下文</returns>
        Task<EventTraceContext> StartTraceAsync(string eventId, string eventType, string? traceId = null);

        /// <summary>
        /// 记录事件处理步骤
        /// </summary>
        /// <param name="traceContext">追踪上下文</param>
        /// <param name="stepName">步骤名称</param>
        /// <param name="duration">耗时</param>
        /// <param name="success">是否成功</param>
        /// <param name="errorMessage">错误信息</param>
        Task RecordStepAsync(EventTraceContext traceContext, string stepName, System.TimeSpan duration, bool success = true, string? errorMessage = null);

        /// <summary>
        /// 完成事件追踪
        /// </summary>
        /// <param name="traceContext">追踪上下文</param>
        /// <param name="success">是否成功</param>
        /// <param name="errorMessage">错误信息</param>
        Task CompleteTraceAsync(EventTraceContext traceContext, bool success = true, string? errorMessage = null);

        /// <summary>
        /// 获取事件追踪信息
        /// </summary>
        /// <param name="eventId">事件ID</param>
        Task<EventTraceInfo?> GetTraceInfoAsync(string eventId);

        /// <summary>
        /// 根据跟踪ID获取相关事件
        /// </summary>
        /// <param name="traceId">跟踪ID</param>
        Task<System.Collections.Generic.List<EventTraceInfo>> GetEventsByTraceIdAsync(string traceId);

        /// <summary>
        /// 获取事件处理性能统计
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="timeRange">时间范围</param>
        Task<EventPerformanceStats> GetPerformanceStatsAsync(string? eventType = null, System.TimeSpan? timeRange = null);
    }

    /// <summary>
    /// 事件追踪上下文
    /// </summary>
    public class EventTraceContext
    {
        /// <summary>
        /// 事件ID
        /// </summary>
        public string EventId { get; set; } = string.Empty;

        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 跟踪ID
        /// </summary>
        public string TraceId { get; set; } = string.Empty;

        /// <summary>
        /// 开始时间
        /// </summary>
        public System.DateTime StartTime { get; set; }

        /// <summary>
        /// 处理步骤列表
        /// </summary>
        public System.Collections.Generic.List<EventTraceStep> Steps { get; set; } = new();

        /// <summary>
        /// 扩展属性
        /// </summary>
        public System.Collections.Generic.Dictionary<string, object> Properties { get; set; } = new();
    }

    /// <summary>
    /// 事件追踪步骤
    /// </summary>
    public class EventTraceStep
    {
        /// <summary>
        /// 步骤名称
        /// </summary>
        public string StepName { get; set; } = string.Empty;

        /// <summary>
        /// 开始时间
        /// </summary>
        public System.DateTime StartTime { get; set; }

        /// <summary>
        /// 耗时
        /// </summary>
        public System.TimeSpan Duration { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 扩展属性
        /// </summary>
        public System.Collections.Generic.Dictionary<string, object> Properties { get; set; } = new();
    }

    /// <summary>
    /// 事件追踪信息
    /// </summary>
    public class EventTraceInfo
    {
        /// <summary>
        /// 事件ID
        /// </summary>
        public string EventId { get; set; } = string.Empty;

        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 跟踪ID
        /// </summary>
        public string TraceId { get; set; } = string.Empty;

        /// <summary>
        /// 开始时间
        /// </summary>
        public System.DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public System.DateTime EndTime { get; set; }

        /// <summary>
        /// 总耗时
        /// </summary>
        public System.TimeSpan TotalDuration { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 处理步骤列表
        /// </summary>
        public System.Collections.Generic.List<EventTraceStep> Steps { get; set; } = new();

        /// <summary>
        /// 扩展属性
        /// </summary>
        public System.Collections.Generic.Dictionary<string, object> Properties { get; set; } = new();
    }

    /// <summary>
    /// 事件性能统计
    /// </summary>
    public class EventPerformanceStats
    {
        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 总处理次数
        /// </summary>
        public long TotalProcessed { get; set; }

        /// <summary>
        /// 成功处理次数
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败处理次数
        /// </summary>
        public long FailureCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalProcessed > 0 ? (double)SuccessCount / TotalProcessed : 0;

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 最小处理时间（毫秒）
        /// </summary>
        public double MinProcessingTimeMs { get; set; }

        /// <summary>
        /// 最大处理时间（毫秒）
        /// </summary>
        public double MaxProcessingTimeMs { get; set; }

        /// <summary>
        /// P95处理时间（毫秒）
        /// </summary>
        public double P95ProcessingTimeMs { get; set; }

        /// <summary>
        /// P99处理时间（毫秒）
        /// </summary>
        public double P99ProcessingTimeMs { get; set; }

        /// <summary>
        /// 统计时间范围
        /// </summary>
        public (DateTime StartTime, DateTime EndTime) TimeRange { get; set; }
    }
}